package next.Functionality;

import next.TemplateActivity.R;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MenuFunctionality extends DialogFunctonality {

		//--Variables--
		protected Integer items;
		protected Integer [] icons;
		protected String [] nameItem;
		protected char [] shortcut;

		//--Own Variables--
		private boolean PrepareToCreate=false;
		
		/** Settings for Menu */
	public void PrepareMenu(Integer items, String [] nameItem, Integer [] icons,char [] shortcut) {
			
		
			PrepareToCreate=false;
			
			//--Default values
			if (items==null || nameItem==null ){
				Log.d("Error Preparing Menu", "Null items or item");
				createToast("Error Preparing Menu--Null items or item");
				return;
			}	
			
			PrepareToCreate=true;
			
			//--Assembling--
		    this.items=items;
			this.icons=icons;
			this.nameItem=nameItem;
			this.shortcut=shortcut;
		}	
		
	/** Called when optionMenu is created. */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		if (PrepareToCreate){
		CreateMenu(menu);
		return true;
		}else return false;
	}
	
	/** Called when optionMenu is Selected. */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuChoice(item);
		
	}
	
	/** Called when contextMenu is created. */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View view,ContextMenuInfo menuInfo)
	{
	super.onCreateContextMenu(menu, view, menuInfo);
	CreateMenu(menu);
	}
	
	/** Called when contextMenu is Selected. */
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
	return MenuChoice(item);
	}
	
	/** Helper method for create menu. */
	private void CreateMenu(Menu menu)
	{
		//--Enable shortcuts in context menu--
			menu.setQwertyMode(true);
		
		for (int i = 0; i < items; i++) {
					
			//--GroupId, itemId,order,Title--
			MenuItem aux=menu.add(0, i+1, i+1, nameItem[i]);
			{
				if (shortcut!=null)
				aux.setAlphabeticShortcut(shortcut[i]);
				if (icons!=null)
				aux.setIcon(icons[i]);
				else
				aux.setIcon(R.drawable.icon);
			}
		}
	}
	
	/** Helper method for selected option menu. */
	private boolean MenuChoice(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			Toast.makeText(this, "You clicked on"+nameItem[item.getItemId()-1]+"", Toast.LENGTH_LONG)
					.show();
			return true;
		case 2:
			Toast.makeText(this, "You clicked on"+nameItem[item.getItemId()-1]+"", Toast.LENGTH_LONG)
					.show();
			return true;
		case 3:
			Toast.makeText(this, "You clicked on"+nameItem[item.getItemId()-1]+"", Toast.LENGTH_LONG)
					.show();
			return true;
		case 4:
			Toast.makeText(this, "You clicked on"+nameItem[item.getItemId()-1]+"", Toast.LENGTH_LONG)
					.show();
			return true;
		case 5:
			Toast.makeText(this, "You clicked on"+nameItem[item.getItemId()-1]+"", Toast.LENGTH_LONG)
					.show();
			return true;
		case 6:
			Toast.makeText(this, "You clicked on"+nameItem[item.getItemId()-1]+"", Toast.LENGTH_LONG)
					.show();
			return true;
		case 7:
			Toast.makeText(this, "You clicked on"+nameItem[item.getItemId()-1]+"", Toast.LENGTH_LONG)
					.show();
			return true;
		}
		return false;
	}

}